package com.xuluoji.urldecoder;

import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class SplashActivity extends AppCompatActivity {
    ImageView view_img_icon = null;
    int count = 0;

    Context context = null;
    ClipboardManager cm = null;

    EditText view_txt_input = null;
    TextView view_txt_output = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 防止点击图标重新启动
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }
        setContentView(R.layout.activity_splash);

        view_img_icon = (ImageView) findViewById(R.id.iv_icon);
        view_img_icon.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                count++;
            }
        });

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (count == 5) {
                    view_img_icon.setVisibility(View.INVISIBLE);
                    viewInit();
                    return;
                }
                Intent it = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(it);
                finish();
            }
        }, 2000);
    }

    private void viewInit() {
        context = getApplicationContext();
        cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);

        // 显示控件
        LinearLayout veil = (LinearLayout) findViewById(R.id.ll_veil);
        veil.setVisibility(View.VISIBLE);

        view_txt_input = (EditText) findViewById(R.id.et_input);
        view_txt_output = (TextView) findViewById(R.id.tv_output);

        Button bt = (Button) findViewById(R.id.bt_paste);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("粘贴 SDK_INT：" + android.os.Build.VERSION.SDK_INT);
                Toast.makeText(context, "粘贴 SDK_INT：" + android.os.Build.VERSION.SDK_INT,
                        Toast.LENGTH_SHORT).show();

                // 粘贴
                if (android.os.Build.VERSION.SDK_INT > 11) {
                    if (!cm.hasPrimaryClip()) {
                        return;
                    } else if (!cm.getPrimaryClipDescription()
                            .hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                        return;
                    }
                    ClipData.Item it = cm.getPrimaryClip().getItemAt(0);
                    view_txt_input.setText(it.coerceToText(context));
                } else {
                    view_txt_input.setText(cm.getText());
                }
            }
        });

        bt = (Button) findViewById(R.id.bt_encode);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CharSequence val = view_txt_input.getText();
                System.out.println("URL编码：" + val.toString());
                Toast.makeText(context, "URL编码", Toast.LENGTH_SHORT).show();

                // 编码
                String url = "";
                try {
                    url = URLEncoder.encode(val.toString(), "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                view_txt_output.setText(url);
            }
        });

        bt = (Button) findViewById(R.id.bt_decode);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CharSequence val = view_txt_input.getText();
                System.out.println("URL解码：" + val.toString());
                Toast.makeText(context, "URL解码", Toast.LENGTH_SHORT).show();

                // 解码
                String url = "";
                try {
                    url = URLDecoder.decode(val.toString(), "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                view_txt_output.setText(url);
            }
        });

        bt = (Button) findViewById(R.id.bt_copy);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("复制 SDK_INT：" + android.os.Build.VERSION.SDK_INT);
                Toast.makeText(context, "复制 SDK_INT：" + android.os.Build.VERSION.SDK_INT,
                        Toast.LENGTH_SHORT).show();

                // 复制
                if (android.os.Build.VERSION.SDK_INT > 11) {
                    cm.setPrimaryClip(ClipData.newPlainText("URL", view_txt_output.getText()));
                } else {
                    cm.setText(view_txt_output.getText());
                }
            }
        });

        bt = (Button) findViewById(R.id.bt_startup1);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(getApplicationContext(), LineSchemeServiceActivity.class);
                startActivity(it);
            }
        });

        bt = (Button) findViewById(R.id.bt_startup2);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(it);
            }
        });

        bt = (Button) findViewById(R.id.bt_startup3);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(getApplicationContext(), LineSchemeServiceActivity.class);
                it.setData(Uri.parse("line://msg/text/startup%3FLineSchemeServiceActivity"));
                startActivity(it);
            }
        });

        bt = (Button) findViewById(R.id.bt_startup4);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(getApplicationContext(), MainActivity.class);
                it.setData(Uri.parse("line://msg/text/startup%3FMainActivity"));
                startActivity(it);
            }
        });

        bt = (Button) findViewById(R.id.bt_startup5);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //jp.gamewith.monst
                Intent it = getPackageManager()
                        .getLaunchIntentForPackage("jp.gamewith.monst");

                //jp.gamewith.gamewith
                if (it == null) {
                    it = getPackageManager()
                            .getLaunchIntentForPackage("jp.gamewith.gamewith");
                }

                if (it != null) {
                    Toast.makeText(context, "启动gw", Toast.LENGTH_SHORT).show();
                    startActivity(it);
                    return;
                }

                Toast.makeText(context, "未安装gw", Toast.LENGTH_SHORT).show();
            }
        });

        bt = (Button) findViewById(R.id.bt_startup6);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //jp.gamewith.gamewith

                Toast.makeText(context, "启动gw", Toast.LENGTH_SHORT).show();

                try {
                    Intent it = new Intent(Intent.ACTION_VIEW);
                    it.setData(Uri.parse("gamewith://walkthrough/monst/recruit/33333starup%3FMainActivity"));
                    startActivity(it);
                } catch (Exception e) {
                    Toast.makeText(context, "未安装gw", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

}

